let Home = {
    init() {
        this.ctx = 'http://192.168.25.55:9999';
        this.charts = {};
        this.loadData();
        this.getEarth3d();//地球
        Public.chartsResize(this.charts);
        this.pureMode();
    },
    loadData() {
        //获取数据总量
        $.ajax({
            headers: {
                "token":Public.token//此处放置请求到的用户token
            },
            url: Public.ctx+'/api/common/innter?dataType=yeesight_pc_homepage_realTime',
            type: 'get',
            dataType: 'json',
            success: (data) => {
                if(data.msg === 'success'){
                    let num2Str = (Math.round(data.data.yeesight[0].totalNum)+"").padStart(9,0);
                    let html = '';
                    num2Str.split('').forEach( (word) => {
                        html += `<span>${word}</span>`
                    });
                    $("#home .totalNum").html(html);
                }
            }
        });

        // 比率
        let preDate = Public.timestampToTime(new Date()- 24*60*60*1000,'-');
        $.ajax({
            headers: {
                "token":Public.token//此处放置请求到的用户token
            },
            url: Public.ctx+ '/api/common/innter?dataType=yeesight_pc_homepage_d2d&params=dataDt::'+preDate,
            type: 'get',
            dataType: 'json',
            success: (data) => {
                if(data.msg === 'success'){
                    let num = data.data.yeesight[0].percent
                    num = isNaN(num)?0:num;
                    if(num<0){
                        $("#home .rate i").addClass("icon-jiantouarrow479")
                    }else {
                        $("#home .rate i").addClass("icon-jiantouarrow504")
                    }
                    num = parseInt(Math.abs(num))+'%';
                    $("#home .rate .num").html(num)


                }
            }
        })

        //获取国家数据
        $.ajax({
            headers: {
                "token":Public.token//此处放置请求到的用户token
            },
            url: Public.ctx+'/api/common/innter?dataType=yeesight_pc_homepage_countryTop10',
            type: 'get',
            dataType: 'json',
            success: (data) => {
                if(data.msg === 'success'){
                    data = data["data"]["yeesight"].slice(0,6);
                    console.log(data);
                    let html = '';
                    data.forEach((item, index) => {
                        html+=`<dd>
                                    <span><img src="${Public.ctx}/images/national_flag/${item.url}.jpg" alt="${item.country}国旗"/>${item.country}</span>
                                    <span class="num">${Public.formatNum(item.num)}</span>
                                </dd>`;
                    });
                    $("#home .country").append(html);
                }
            }
        })
        //获取领域数据
        $.ajax({
            headers: {
                "token":Public.token//此处放置请求到的用户token
            },
            url: Public.ctx+'/api/common/innter?dataType=yeesight_pc_homepage_category',
            type: 'get',
            dataType: 'json',
            success: (data) => {
                const iconObj={
                    zhengzhi:'icon-politics',
                    shehui:'icon-shehuishuju',
                    keji:'icon-keji1',
                    jingji:'icon-jingjifazhan',
                    nengyuan:'icon-energy_03',
                    nongye:'icon-nongye3',
                };
                if(data.msg === 'success') {
                    let html = '';
                    let obj = data.data;
                    let arr = [];
                    Object.keys(obj).forEach((item,index)=>{
                        let name =  obj[item][0].class;
                        let num = obj[item].reduce((pre,cur)=>{
                            return Number(cur.value)+pre
                        },0);
                        arr.push({
                            className:item,
                            name,
                            num,
                        })
                    });
                    arr.sort((a,b)=>{// 按value总和排序
                       return b.num-a.num
                    });
                    arr.forEach(item=>{
                        html+=`<dd class="${item.className}"><span><i class="iconfont ${iconObj[item.className]}"></i>${item.name}</span><span class="num">${Math.round(item.num)}</span></dd>`
                    });
                    $("#home .field").append(html);
                }
            }
        })
    },
    getEarth3d() {
        let that = this;
        //飞线数据
        let myChart = echarts.init(document.getElementById('ec01_sphere'));
        this.charts.ec01_sphere = myChart;
        let isDeathData = false;//是否假数
        let url = isDeathData?'../../mock/HomePage_earth3d_data.json':this.ctx + '/api/index/news/map';
        let mapDataScatters = [];
        let mapDataLines = [];
        let isDataReady = 0;
        $.ajax({
            headers: {
                "token": Public.token//此处放置请求到的用户token
            },
            url,
            type: 'get',
            dataType: 'json',
            success(data){
                const dataFromInterface = isDeathData?data.data:data.data.maps;
                const coordsXiAn = [109.1162, 34.2004]; //西安经纬度
                // 散点数据
                mapDataScatters = dataFromInterface.concat([{
                    name: '西安',
                    latitude: 109.1162,
                    longitude: 34.2004
                }]).map(function (item) {
                    return {
                        // name: item.name,
                        // value: [item.latitude, item.longitude]
                        name:isDeathData? item.name:item.areacity,
                        value:isDeathData?[item.latitude, item.longitude]:[item.citylatitude,item.citylongitude]
                    }
                });
                // 连线数据
                mapDataLines = dataFromInterface.map(function (item) {
                    return {
                        // name: item.name,
                        // coords: [[item.latitude, item.longitude], coordsXiAn]
                        name:isDeathData?item.name:item.areacity,
                        coords:isDeathData?[[item.latitude, item.longitude], coordsXiAn]:[[item.citylatitude,item.citylongitude], coordsXiAn]
                    }
                }).splice(0,10);
            },complete:function(){
                isDataReady = isDataReady +1;
                if(isDataReady === 2){
                    drawEarth3d(mapDataScatters,mapDataLines,scattersData); //热点数据 scattersData、jsonCity
                }
            }
        });
        //点数据
        let isRealData = false;//是假数?
        let scattersUrl = isRealData?'../../mock/population.json':this.ctx + '/api/index/news/map';
        let scattersData = [];
        $.ajax({
            headers: {
                "token": Public.token//此处放置请求到的用户token
            },
            url:scattersUrl,
            data:{
                pageSize:isRealData?'':10000,
            },
            type: 'get',
            dataType: 'json',
            success:function(data){
                if(isRealData){
                    scattersData = data.filter(function (dataItem) {
                        return dataItem[2] >  8;
                    }).map(function (dataItem) {
                        return {
                            value: [dataItem[0]+Math.random()*2, dataItem[1]+Math.random()],
                            symbolSize:Math.random()*3
                        }
                    });
                }else {
                    let items=data.data.maps;
                    scattersData = items.map(function (item) {
                        return {
                            name:item.areacity,
                            value:[Number(item.citylongitude)+Math.random()*8,Number(item.citylatitude)+Math.random()*9],
                            symbolSize:Math.random()*3
                        }
                    });
                }
            },complete:function(){
                isDataReady = isDataReady +1;
                if(isDataReady === 2){
                    drawEarth3d(mapDataScatters,mapDataLines,scattersData);
                }
            }
        });
        drawEarth3d(mapDataScatters,mapDataLines);
        function drawEarth3d(attackPoints,attackLines,scattersData ) {
            // 截取需要连线的点
            // var inCity = mapDataLines.slice(-5);
            let inCity = attackLines;
            let series = [
                {//绘制热点数据点
                    type: 'scatter3D',
                    coordinateSystem: 'globe',
                    blendMode: 'lighter',
                    symbolSize: 2,
                    itemStyle: {
                        color: 'rgba(87, 250, 250, 0.3)',
                        opacity: 1,
                        shadowColor: 'rgba(255, 0, 0, 0.8)',
                        shadowBlur: 30
                    },
                    label: {
                        show: false,//不知如何取消鼠标移上去后触发的动作
                        formatter: '{b}',
                        distance: 15
                    },
                    data: scattersData
                },{//绘制攻击点
                    type: 'scatter3D',
                    coordinateSystem: 'globe',
                    // blendMode: 'lighter',
                    symbolSize: 3,
                    itemStyle: {
                        color: 'rgba(153, 51, 255, 0)',
                        opacity: .8,
                        borderWidth:'1',
                        borderColor:'#ff3ecf'
                    },
                    label: {
                        show: false,//不知如何取消鼠标移上去后触发的动作
                        formatter: '{b}',
                        distance: 15
                    },
                    data: attackPoints
                },
                {//绘制攻击线
                    type: 'lines3D',
                    effect: {
                        show: true,
                        trailWidth: 2,
                        trailLength: 0.55,
                        trailOpacity: .7,
                        trailColor: '#ff3ecf'
                    },

                    lineStyle: {
                        width: 2,
                        color: '#6e0fff',
                        //color: '#f8ff8b',
                        opacity: .6
                    },
                    // blendMode: 'lighter',

                    data: inCity
                }
            ];

            myChart.setOption({//地球背景图

                globe: {
                    baseTexture: '../static/imgs/earth3d.jpg',
                    globeRadius: 80,
                    globeOuterRadius:10,
                    viewControl: {
                        // autoRotate: false,
                        autoRotateSpeed: 9,
                        alpha: 33,
                        beta: 135,
                        // targetCoord: [87.68, 43.77]//可以定位到乌鲁木齐等城市
                    },
                    light: { // 主光源,影响地图颜色
                        ambient: {
                            intensity: 1
                        },
                        main: {
                            intensity: 1
                        }
                    },
                },
                tooltip: {
                    show: false,
                },
                series: series
            });
        }
    },
    pureMode(){
        $("#pureMode").click(function () {
            $(".chartWrap dl").toggle();
        })
    }
};
Home.init();